package hxy.dragon.secure

import kotlin.test.*

class OpenSSHPrivateKeyParsingTest {
    @Test
    fun testParseOpenSSHPrivateKey() {
        // 测试OpenSSH私钥解析
        val openSSHPrivateKey = """-----BEGIN OPENSSH PRIVATE KEY-----
b3BlbnNzaC1rZXktdjEAAAAABG5vbmUAAAAEbm9uZQAAAAAAAAABAAAAMwAAAAtzc2gtZW
QyNTUxOQAAACD0hDk1an7ZXMc9uKD/Hq27Kv16hfXDkoybvcx+AQiYwgAAAJi5zU0Tuc1N
EwAAAAtzc2gtZWQyNTUxOQAAACD0hDk1an7ZXMc9uKD/Hq27Kv16hfXDkoybvcx+AQiYwg
AAAED4NfJ/T2amYrgStidoIsphfaFc30lGax/C4RA6YC1be/SEOTVqftlcxz24oP8erbsq
/XqF9cOSjJu9zH4BCJjCAAAAE2FvaGFuaG9uZ3poaUBxcS5jb20BAg==
-----END OPENSSH PRIVATE KEY-----"""
        
        val parsedKey = parseOpenSSHPrivateKey(openSSHPrivateKey)
        
        // 验证解析出的私钥长度为32字节
        assertEquals(32, parsedKey.size)
        
        // 验证解析出的私钥不为空
        assertTrue(parsedKey.any { it != 0.toByte() })
    }
    
    @Test
    fun testParseInvalidOpenSSHPrivateKey() {
        // 测试无效的OpenSSH私钥
        val invalidPrivateKey = "invalid key"
        
        assertFailsWith<IllegalArgumentException> {
            parseOpenSSHPrivateKey(invalidPrivateKey)
        }
    }
    
    @Test
    fun testParseEncryptedOpenSSHPrivateKey() {
        // 测试加密的OpenSSH私钥（应该抛出异常）
        // 这是一个模拟的加密私钥，cipher name不是"none"
        val encryptedPrivateKey = """-----BEGIN OPENSSH PRIVATE KEY-----
b3BlbnNzaC1rZXktdjEAAAAACmFlczI1Ni1jYmMAAAAGYmNyeXB0AAAAGAAAABBKHQ8sFP3l
R5HhQY7VNOO/AAAAEAAAAAEAAAAzAAAAC3NzaC1lZDI1NTE5AAAAQMS7nkbJ517kWcDgKv4p
1aQOjZ3A1wzu864FGTJLVgCfJ03Z6JmF48wlFr70HJSC5K12wHq79n93v58lY2rj5Q0A
-----END OPENSSH PRIVATE KEY-----"""
        
        assertFailsWith<IllegalArgumentException> {
            parseOpenSSHPrivateKey(encryptedPrivateKey)
        }
    }
}